package cn.zifangsky.manager.impl;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.zifangsky.common.PageInfo;
import cn.zifangsky.manager.UsrUserManager;
import cn.zifangsky.mapper.UsrUserMapper;
import cn.zifangsky.model.UsrUser;
import cn.zifangsky.utils.EncryptionUtil;

@Service("usrUserManager")
public class UsrUserManagerImpl implements UsrUserManager {

	@Autowired
	private UsrUserMapper userMapper;

	public int deleteByPrimaryKey(Integer id) {
		return userMapper.deleteByPrimaryKey(id);
	}

	public int insert(UsrUser usrUser) {
		return userMapper.insert(usrUser);
	}

	public int insertSelective(UsrUser usrUser) {
		return userMapper.insertSelective(usrUser);
	}

	public UsrUser selectByPrimaryKey(Integer id) {
		return userMapper.selectByPrimaryKey(id);
	}

	public int updateByPrimaryKeySelective(UsrUser usrUser) {
		return userMapper.updateByPrimaryKeySelective(usrUser);
	}

	public int updateByPrimaryKey(UsrUser usrUser) {
		return userMapper.updateByPrimaryKey(usrUser);
	}

	public int findAllCount(UsrUser usrUser) {
		return userMapper.findAllCount(usrUser);
	}

	public List<UsrUser> findAll(PageInfo pageInfo, UsrUser usrUser) {
		return userMapper.findAll(pageInfo, usrUser);
	}

	public UsrUser login(String username, String password) {
		UsrUser result = new UsrUser();
		if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
			// 密码采用sha256加密
			result = userMapper.selectByNamePasswd(username, EncryptionUtil.sha256Hex(password));
		}

		return result;
	}

	public void logout(HttpSession session) {
		session.removeAttribute("user");
	}

}
